package zirao.base.manager;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import java.util.Stack;

/** 
 * @ClassName: BaseActivityManager
 * @Description: Activity管理类：用于管理Activity和退出程序 
 * @author lixiaonan
 * @date 2016-10-18
 */
public class BaseActivityManager {
    private static Stack<Activity> mActivityStack;   		// Activity栈
    private static BaseActivityManager 	mInstance;				// 单例模式对象
    
    /** 
     * 创建单一实例 
     */  
    public static BaseActivityManager getAppManager() {
        if (mInstance == null) {  
        	mInstance = new BaseActivityManager();
        }  
        return mInstance;  
    }

    /**
     * 添加Activity到堆栈
     */
    public int getCount() {
        if (mActivityStack == null) {
            mActivityStack = new Stack<Activity>();
        }
        return mActivityStack.size();
    }

    /** 
     * 添加Activity到堆栈 
     */  
    public void addActivity(Activity activity) {
        if (mActivityStack == null) {  
            mActivityStack = new Stack<Activity>();
        }  
        mActivityStack.push(activity);
    }  
  
    /** 
     * 获取当前Activity（堆栈中最后一个压入的） 
     */  
    public Activity currentActivity() {
    	Activity activity = mActivityStack.lastElement();
        return activity;  
    }  
  
    /** 
     * 结束当前Activity（堆栈中最后一个压入的） 
     */  
    public void finishActivity() {  
    	Activity activity = mActivityStack.lastElement();
        mActivityStack.pop();
        finishActivity(activity);  
    }  
  
    /** 
     * 结束指定的Activity 
     */  
    public void finishActivity(Activity activity) {
        if (activity != null) {  
            mActivityStack.remove(activity);  
            activity.finish();  
            activity = null;  
        }  
    }  
  
    /** 
     * 结束指定类名的Activity 
     */  
    public void finishActivity(Class<?> cls) {
        for (Activity activity : mActivityStack) {
            if (activity.getClass().equals(cls)) {  
                finishActivity(activity);  
            }  
        }  
    }

    /**
     * 结束指定类名的Activity
     */
    public boolean isActivityExist(Class<?> cls) {
        for (Activity activity : mActivityStack) {
            if (activity.getClass().equals(cls)) {
                return true;
            }
        }

        return false;
    }

    /** 
     * 结束所有Activity 
     */  
    public void finishAllActivity() {  
        for (int i = 0; i < mActivityStack.size(); i++) {  
            if (null != mActivityStack.get(i)) {  
                mActivityStack.get(i).finish();  
            }  
        }  
        mActivityStack.clear();  
    }  
  
    /** 
     * 退出应用程序 
     */  
    public void AppExit(Context context) {
        try {  
            finishAllActivity();
            ActivityManager activityMgr = (ActivityManager) context
                    .getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.killBackgroundProcesses(context.getPackageName());  
            System.exit(0);
        } catch (Exception e) {
        }  
    }  
}